package cn.zime.iot.view;

import cn.zime.iot.entity.User;
import cn.zime.iot.entity.UserDao;
import cn.zime.iot.util.DBUtil;
import cn.zime.iot.util.StringUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Objects;

public class ChangePassword extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTxt;
	private JPasswordField passwordTxt;
	private JPasswordField re_passwordTxt;
	private JPasswordField old_password;
	private UserDao userDao = new UserDao();
	private DBUtil dbUtil = new DBUtil();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			try {
				ChangePassword frame = new ChangePassword();
				frame.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ChangePassword() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 500, 403);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("修改密码");
		lblNewLabel.setFont(new Font("2445黑", Font.PLAIN, 20));
		lblNewLabel.setBounds(208, 20, 96, 34);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("账号：");
		lblNewLabel_1.setIcon(new ImageIcon(Objects.requireNonNull(ChangePassword.class.getResource("/image/账号.png"))));
		lblNewLabel_1.setFont(new Font("华文行楷", Font.PLAIN, 13));
		lblNewLabel_1.setBounds(107, 77, 65, 15);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("新密码：");
		lblNewLabel_2.setIcon(new ImageIcon(Objects.requireNonNull(ChangePassword.class.getResource("/image/新密码.png"))));
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 13));
		lblNewLabel_2.setBounds(107, 130, 76, 15);
		contentPane.add(lblNewLabel_2);

		JLabel lblNewLabel_3 = new JLabel("确认\u5BC6\u7801\uFF1A");
		lblNewLabel_3.setIcon(new ImageIcon(Objects.requireNonNull(ChangePassword.class.getResource("/image/新密码.png"))));
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 13));
		lblNewLabel_3.setBounds(107, 195, 87, 15);
		contentPane.add(lblNewLabel_3);

		JLabel lblNewLabel_4 = new JLabel("原密码");
		lblNewLabel_4.setIcon(new ImageIcon(Objects.requireNonNull(ChangePassword.class.getResource("/image/验证码.png"))));
		lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 13));
		lblNewLabel_4.setBounds(107, 252, 76, 15);
		contentPane.add(lblNewLabel_4);

		JButton btnNewButton = new JButton("确认");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				regActionPerformed(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(Objects.requireNonNull(ChangePassword.class.getResource("/image/确认.png"))));
		btnNewButton.setBounds(124, 303, 93, 23);
		contentPane.add(btnNewButton);

		userNameTxt = new JTextField();
		userNameTxt.setBounds(204, 75, 151, 21);
		contentPane.add(userNameTxt);
		userNameTxt.setColumns(10);

		passwordTxt = new JPasswordField();
		passwordTxt.setBounds(204, 128, 151, 21);
		contentPane.add(passwordTxt);
		passwordTxt.setColumns(10);

		re_passwordTxt = new JPasswordField();
		re_passwordTxt.setBounds(204, 193, 151, 21);
		contentPane.add(re_passwordTxt);
		re_passwordTxt.setColumns(10);

		old_password = new JPasswordField();
		old_password.setBounds(204, 250, 151, 21);
		contentPane.add(old_password);
		old_password.setColumns(10);

		JButton cancelBtn = new JButton("取消");
		cancelBtn.setIcon(new ImageIcon(Objects.requireNonNull(ChangePassword.class.getResource("/image/取消.png"))));
		cancelBtn.setBounds(282, 303, 93, 23);
		contentPane.add(cancelBtn);
		cancelBtn.addActionListener(e -> {
			this.dispose();
		});
		setIconImage(new ImageIcon(Objects.requireNonNull(RegisterAccountView.class.getResource("/image/1.png"))).getImage());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
		setLocationRelativeTo(null);
	}
	private void regActionPerformed(ActionEvent e) {
		String userName=this.userNameTxt.getText();
		String passwordTxt=new String(this.passwordTxt.getPassword());
		String re_passwordTxt=new String(this.re_passwordTxt.getPassword());
		String old_password=new String(this.old_password.getPassword());

		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}if(StringUtil.isEmpty(passwordTxt)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}if(StringUtil.isEmpty(re_passwordTxt)) {
			JOptionPane.showMessageDialog(null, "确认密码不能为空！");
			return;
		}
		if(StringUtil.isEmpty(old_password)) {
			JOptionPane.showMessageDialog(null, "原密码不能为空！");
			return;
		}
		if(passwordTxt.equals(re_passwordTxt)) {
			Connection con=null;
			User user=new User(userName,passwordTxt,old_password);
			try {
				con=dbUtil.getConn();
				int addNum=userDao.changePassWord(con, user);
				if(addNum==1) {
					JOptionPane.showMessageDialog(null, "修改成功！");
				}else {
					JOptionPane.showMessageDialog(null, "账号或原密码不正确！");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}finally {
				dbUtil.closeConn(con);
			}
		}else {
			JOptionPane.showMessageDialog(null, "两次密码不一致！");
			return;
		}

	}
}
